<?php

    header("content-type:text/html;charset=utf-8");
    $redis = new Redis();
    $redis->connect("127.0.0.1","6379");
    $id = $_COOKIE['userid'];
    $goods_id = $_POST['goods_id'];
    $score = $_POST['score'];
    $pdo = new PDO("mysql:host=127.0.0.1;dbname=exam","root","root");
    $sql2 = "select number from goods where id=$goods_id";
    $number = $pdo->query($sql2)->fetch();
    // redis 存入数据 只需要执行一遍
    // for($i=1;$i<=$number['number'];$i++){
    // $redis->lpush("data".$goods_id,$i);
    // }
    if($redis->lpop("data".$goods_id)){
        if($number['number']>0){
            $sql = "update goods set number = number-1 where id=$goods_id";
            $res = $pdo->exec($sql);
            if($res){
                $time = date("Y-m-d");
                $sql1 = "update admin set score=score-$score";
                $ret = $pdo->exec($sql1);
                $sql3 = "insert into score values('','$id','换购商品','$goods_id','$score','$time')";
                $pdo->exec($sql3);
                if($res){
                    $data = array('code' =>1 , 'msg'=>"换购成功");
                }
            }
        }
    }else{
        $data = array('code' => 0 ,'msg'=>"换购失败");
    }
    echo json_encode($data);
    
    